package com.hezhi.project.module.student.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hezhi.project.module.student.mapper.StudentMapper;
import com.hezhi.project.module.classes.mapper.ClassesMapper;
import com.hezhi.project.module.parent.domain.Parent;
import com.hezhi.project.module.parent.mapper.ParentMapper;
import com.hezhi.project.module.student.domain.Student;
import com.hezhi.project.module.student.service.IStudentService;
import com.hezhi.project.module.studentIc.domain.Card;
import com.hezhi.project.module.studentIc.domain.StudentIc;
import com.hezhi.project.module.studentIc.mapper.StudentIcMapper;
import com.alibaba.fastjson.JSONObject;
import com.hezhi.common.support.Convert;
import com.hezhi.common.utils.CommonUtil;
import com.hezhi.framework.aspectj.LogAspect;
import com.hezhi.framework.config.MyConfig;

/**
 * 学生 服务层实现
 * 
 * @author hezhi
 * @date 2018-08-06
 */
@Service
public class StudentServiceImpl implements IStudentService 
{
	@Autowired
	private StudentMapper studentMapper;
	@Autowired
	private StudentIcMapper studentIcMapper;
	@Autowired
	private ParentMapper parentMapper;
	@Autowired
	private ClassesMapper classesMapper;
	@Autowired
	private MyConfig myConfig;
	private static final Logger log = LoggerFactory.getLogger(LogAspect.class);

	/**
     * 查询学生信息
     * 
     * @param studentNumber 学生ID
     * @return 学生信息
     */
    @Override
	public Student selectStudentById(String studentNumber)
	{
	    return studentMapper.selectStudentById(studentNumber);
	}
	
	/**
     * 查询学生列表
     * 
     * @param student 学生信息
     * @return 学生集合
     */
	@Override
	public List<Student> selectStudentList(Student student)
	{
	    return studentMapper.selectStudentList(student);
	}
	
    /**
     * 新增学生
     * 
     * @param student 学生信息
     * @return 结果
     */
	@Override
	public int insertStudent(Student student)
	{
	    return studentMapper.insertStudent(student);
	}
	
	/**
     * 修改学生
     * 
     * @param student 学生信息
     * @return 结果
     */
	@Override
	public int updateStudent(Student student)
	{
	    return studentMapper.updateStudent(student);
	}
	
	/**
     * 保存学生
     * 
     * @param student 学生信息
     * @return 结果
     */
	@Override
	@Transactional
	public int saveStudent(Student student)
	{
		boolean ifNew = student.getIfNew();
		int rows = 0;
		if (ifNew)
        {
			//新增学生
			rows = studentMapper.insertStudent(student);
			//新增卡号
			StudentIc studentIc = student.getStudentIc();
			studentIc.setStudentNumber(student.getStudentNumber());
			studentIc.setStatus("1");//对接下发接口
			studentIcMapper.insertStudentIc(studentIc);
			List<Card> listCard = new ArrayList<>();
			Card card = new Card();
			card.setCardNo(studentIc.getIcNumber());
			card.setName(student.getName());
			card.setStatus("1");
			card.setStudentNumber(student.getStudentNumber());
			card.setTeam(classesMapper.selectClassesById(student.getClassNumber()).getName());
			listCard.add(card);
			//studentscard(listCard);
			//新增家长
			for(Parent parent:student.getParents()){
				parent.setStudentNumber(student.getStudentNumber());
				parentMapper.insertParent(parent);
			}
		}
		else
        {
			//修改学生信息
			rows = studentMapper.updateStudent(student);
			//IC卡号信息不给修改
			//如果家长绑定了微信 不给修改
			for(Parent parent:student.getParents()){
				parent.setStudentNumber(student.getStudentNumber());
				if("".equals(parent.getParentId()) || parent.getParentId() == null){
					parentMapper.insertParent(parent);
				}else if(parent.getOpenId() == null || "".equals(parent.getOpenId())){
					parentMapper.updateParent(parent);
				}
			}
        }
		return rows;
	}
	
	/**
     * 删除学生对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteStudentByIds(String ids)
	{
		return studentMapper.deleteStudentByIds(Convert.toStrArray(ids));
	}
	
	public boolean studentscard(List<Card> listCard) {
		JSONObject j = CommonUtil.httpRequest(myConfig.getMachineurl()+"/studentscard", "POST", JSONObject.toJSONString(listCard));
		if("success".equals(j.getString("status"))){
			return true;
		}else{
			log.error(JSONObject.toJSONString(j));
			return false;
		}
	}
	
}
